﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bluyel.Core
{
    /// <summary>
    /// Factory to build Data Named.
    /// </summary>
    public class FactoryData
    {
        //=====================================================================
        #region Constructor.

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        public FactoryData()
        {

        }

        #endregion

        //=====================================================================
        #region Public Create Data Basic methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a data named of int type.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="varOrConst"></param>
        /// <returns></returns>
        public DInt CreateDInt(string name, ITypeBasic type, VarOrConst varOrConst, int val)
        {
            // check params
            if (string.IsNullOrWhiteSpace(name))
                return null;
            if (type == null)
                return null;

            // create a DVal and set it to the data named
            ValueInt dval = new ValueInt(val);
            DInt dint= new DInt(name, type, varOrConst);

            dint.SetDVal(dval);
            return dint;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a data named of string type.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="varOrConst"></param>
        /// <returns></returns>
        public DString CreateDString(string name, ITypeBasic type, VarOrConst varOrConst, string val)
        {
            // check params
            if (string.IsNullOrWhiteSpace(name))
                return null;
            if (type == null)
                return null;

            // TODO: manage the case when string is null (nullable?)
            if (val == null)
                return null;

            // create a DVal and set it to the data named
            ValueString dval = new ValueString(val);

            DString dstring=  new DString(name, type, varOrConst);
            dstring.SetDVal(dval);
            return dstring;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a data named of bool type.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="varOrConst"></param>
        /// <returns></returns>
        public DBool CreateDBool(string name, ITypeBasic type, VarOrConst varOrConst, bool val)
        {
            // check params
            if (string.IsNullOrWhiteSpace(name))
                return null;
            if (type == null)
                return null;

            // create a DVal and set it to the data named
            ValueBool dval = new ValueBool(val);

            DBool dbool = new DBool(name, type, varOrConst);

            dbool.SetDVal(dval);
            return dbool;
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a data named of double type.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="varOrConst"></param>
        /// <returns></returns>
        public DDouble CreateDDouble(string name, ITypeBasic type, VarOrConst varOrConst, double val)
        {
            // check params
            if (string.IsNullOrWhiteSpace(name))
                return null;
            if (type == null)
                return null;

            // create a DVal and set it to the data named
            ValueDouble dval = new ValueDouble(val);

            DDouble  ddouble= new DDouble(name, type, varOrConst);
            ddouble.SetDVal(dval);
            return ddouble;
        }


        //---------------------------------------------------------------------
        /// <summary>
        /// Create a data named of basic type.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="varOrConst"></param>
        /// <returns></returns>
        public IDBasic CreateDBasic(string name, ITypeBasic type, VarOrConst varOrConst)
        {
            // check params
            if (string.IsNullOrWhiteSpace(name))
                return null;
            if (type == null)
                return null;

            if (type.Id.IdAreEquals(TypeDef.TypeIntId))
                return CreateDInt(name, type, varOrConst,TypeDef.DefValInt);

            if (type.Id.IdAreEquals(TypeDef.TypeStringId))
                return CreateDString(name, type, varOrConst, TypeDef.DefValString);

            if (type.Id.IdAreEquals(TypeDef.TypeDoubleId))
                return CreateDDouble(name, type, varOrConst, TypeDef.DefValDouble);

            if (type.Id.IdAreEquals(TypeDef.TypeBoolId))
                return CreateDBool(name, type, varOrConst, TypeDef.DefValBool);

            return null;
        }

        #endregion
    }
}
